/**
 * 
 */
package org.swing.utility.sytem.jar;

import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author lqnhu
 *
 */
public class JavaVersion {
	private final int major;
	private final int middle;
	private final int minor;
	private final int update;

	public JavaVersion() {
		this(System.getProperty("java.version"));
	}

	public JavaVersion(int major, int middle, int minor, int update) {
		this.major = major;
		this.middle = middle;
		this.minor = minor;
		this.update = update;
	}

	JavaVersion(String version) {
		int[] versions = getVersions(version);
		this.major = versions[0];
		this.middle = versions[1];
		this.minor = versions[2];
		if (3 < versions.length) {
			this.update = versions[3];
		} else {
			this.update = 0;
		}
	}

	private static int[] getVersions(String version) {
		StringTokenizer st = new StringTokenizer(version, "._");
		int[] versions = new int[st.countTokens()];
		for (int i = 0; st.hasMoreTokens(); i++) {
			versions[i] = Integer.parseInt(st.nextToken());
		}
		return versions;
	}

	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}

		if (!(obj instanceof JavaVersion)) {
			return false;
		}

		JavaVersion version = (JavaVersion) obj;

		return major == version.major && middle == version.middle
				&& minor == version.minor && update == version.update;
	}

	public int hashCode() {
		int result = 17;
		result = 37 * result + major;
		result = 37 * result + middle;
		result = 37 * result + minor;
		result = 37 * result + update;
		return result;
	}

	public String toString() {
		return major + "." + middle + "." + minor + "_" + update;
	}

	public final List<String> KEYWORDS = Arrays.asList("abstract",
			"assert", "boolean", "break", "byte", "case", "catch", "char",
			"class", "const", "continue", "default", "do", "double", "else",
			"enum", "extends", "false", "final", "finally", "float", "for",
			"goto", "if", "implements", "import", "instanceof", "int",
			"interface", "long", "native", "new", "null", "package", "private",
			"protected", "public", "return", "short", "static", "strictfp",
			"super", "switch", "synchronized", "this", "throw", "throws",
			"transient", "true", "try", "void", "volatile", "while");

	public boolean isIdentifier(CharSequence seq) {
		int len = seq.length();
		if (len == 0)
			return false;
		if (!Character.isJavaIdentifierStart(seq.charAt(0)))
			return false;
		for (int i = 1; i < len; i++) {
			if (!Character.isJavaIdentifierPart(seq.charAt(i)))
				return false;
		}
		return true;
	}

	public int compareTo(Object obj) {
		JavaVersion version = (JavaVersion) obj;
		if (major > version.major) {
			return 1;
		}
		if (major < version.major) {
			return -1;
		}

		if (middle > version.middle) {
			return 1;
		}
		if (middle < version.middle) {
			return -1;
		}

		if (minor > version.minor) {
			return 1;
		}
		if (minor < version.minor) {
			return -1;
		}

		if (update > version.update) {
			return 1;
		}
		if (update < version.update) {
			return -1;
		}
		return 0;
	}

}
